package com.modakai.med.hosp.controller;

import com.modakai.med.common.MD5;
import com.modakai.med.common.exception.MedException;
import com.modakai.med.common.helper.HttpRequestHelper;
import com.modakai.med.common.result.Result;
import com.modakai.med.common.result.ResultCodeEnum;
import com.modakai.med.hosp.service.DepartmentService;
import com.modakai.med.hosp.service.HospitalService;
import com.modakai.med.hosp.service.HospitalSetService;
import com.modakai.med.hosp.service.ScheduleService;
import com.modakai.med.vo.DepartmentQueryVo;
import com.modakai.med.vo.ScheduleQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Tag(name = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
@RequiredArgsConstructor
public class ApiController {

    private final HospitalService hospitalService;

    private final DepartmentService departmentService;

    private final ScheduleService scheduleService;

    private final HospitalSetService hospitalSetService;

    @Operation(summary = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        validateSignAndHoscode(paramMap);

        hospitalService.save(paramMap);
        return Result.ok();

    }

    @Operation(summary = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String)paramMap.get("logoData");
        if(!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll("", "+");
            paramMap.put("logoData", logoData);
        }

        validateSignAndHoscode(paramMap);

        departmentService.save(paramMap);
        return Result.ok();
    }

    @Operation(summary = "获取医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new MedException(ResultCodeEnum.PARAM_ERROR);
        }

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll("", "+");
            paramMap.put("logoData", logoData);
        }


        String signKey = hospitalSetService.getSignKey(hoscode);
        String sign = (String) paramMap.get("sign");
        //签名校验
        String signKeyMd5 = MD5.encrypt(signKey);
        if (!sign.equalsIgnoreCase(signKeyMd5)) {
            throw new MedException(ResultCodeEnum.SIGN_ERROR);
        }

        return Result.ok(hospitalService.getByHoscode((String) paramMap.get("hoscode")));
    }

    @Operation(summary = "查询科室列表")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        //非必填
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty((String) paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty((String) paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        validateSignAndHoscode(paramMap);

        // 分页查询
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        return Result.ok(departmentService.selectPage(page, limit, departmentQueryVo));
    }

    @Operation(summary = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        //非必填
        String depcode = (String) paramMap.get("depcode");

        validateSignAndHoscode(paramMap);

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    @Operation(summary = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        validateSignAndHoscode(paramMap);

        scheduleService.save(paramMap);
        return Result.ok();
    }

    @Operation(summary = "分页查询排班")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        //非必填
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty((String) paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty((String) paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        validateSignAndHoscode(paramMap);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        return Result.ok(scheduleService.selectPage(page, limit, scheduleQueryVo));
    }

    @Operation(summary = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");

        String hosScheduleId = (String) paramMap.get("hosScheduleId");

        validateSignAndHoscode(paramMap);

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


    private Map<String, Object> validateSignAndHoscode(Map<String, Object> paramMap) {

        //必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
        if (StringUtils.isEmpty(hoscode)) {
            throw new MedException(ResultCodeEnum.PARAM_ERROR);
        }
        //签名校验
        String signKey = hospitalSetService.getSignKey(hoscode);
        String sign = (String) paramMap.get("sign");

        if (!sign.equalsIgnoreCase(MD5.encrypt(signKey))) {
            throw new MedException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }

}
